package com.ddz.utils;

import com.ddz.pojo.User;
import org.springframework.util.DigestUtils;

import java.util.List;

public class MD5Util {
    static String salt = "qwer";//盐
    static Integer num = 1;//散列次数
    public static void main(String[] args){
        String password = "123";//
        String salt = "qwer";//盐
        Integer num = 1;//散列次数
        String pwd = DigestUtils.md5DigestAsHex((password+salt).getBytes());
//        pwd = DigestUtils.md5DigestAsHex((password+salt+num).getBytes());
        System.out.println(pwd);
        //f56fa937f0eb2e484eba3480b75850e9
    }
    //加密user的密码
    public static User createMD5User(User user){
        user.setPassword(DigestUtils.md5DigestAsHex((user.getPassword()+salt+num).getBytes()));
        return user;
    }

    //加密userList的密码
    public static List<User> createMD5UserList(List<User> userList){
        for(User user : userList) {
            user.setPassword(DigestUtils.md5DigestAsHex((user.getPassword() + salt + num).getBytes()));
        }
        return userList;
    }

    //加密字符串
    public static String createMD5Str(String str){
        return DigestUtils.md5DigestAsHex((str + salt + num).getBytes());

    }

}
